package cn.jcet.module.bus.controller;


import cn.jcet.common.utils.DataGridView;
import cn.jcet.common.utils.ResultObj;
import cn.jcet.module.bus.domain.Goods;
import cn.jcet.module.bus.domain.Outport;
import cn.jcet.module.bus.domain.Provider;
import cn.jcet.module.bus.service.IGoodsService;
import cn.jcet.module.bus.service.IOutportService;
import cn.jcet.module.bus.service.IProviderService;
import cn.jcet.module.bus.vo.OutportVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鲜磊
 * @since 2019-12-25
 */
@RestController
@RequestMapping("/outport")
public class OutportController {


    @Autowired
    private IOutportService outportService;

    @Autowired
    private IProviderService providerService;

    @Autowired
    private IGoodsService goodsService;

    /**
     * 添加退货单
     * @param id
     * @param number
     * @param remark
     * @return
     */
    @PostMapping("addOutport")
    public ResultObj addOutport(Integer id,Integer number,String remark){
        try {

            this.outportService.addOutport(id,number,remark);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }


    /**
     * 加载退货订单
     * @param outportVo
     * @return
     */
    @GetMapping("loadAllOutport")
    public DataGridView loadAllOutport(OutportVo outportVo){
        IPage<Outport> page = new Page<>(outportVo.getPage(),outportVo.getLimit());

        QueryWrapper<Outport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(outportVo.getProviderid()!=null&&outportVo.getProviderid()!=0,"providerid",outportVo.getProviderid());
        queryWrapper.eq(outportVo.getGoodsid()!=null&&outportVo.getGoodsid()!=0,"goodsid",outportVo.getGoodsid());
        queryWrapper.ge(outportVo.getStartTime()!=null, "outputtime", outportVo.getStartTime());
        queryWrapper.le(outportVo.getEndTime()!=null, "outputtime", outportVo.getEndTime());
        queryWrapper.like(StringUtils.isNotBlank(outportVo.getOperateperson()), "operateperson", outportVo.getOperateperson());
        queryWrapper.like(StringUtils.isNotBlank(outportVo.getRemark()), "remark", outportVo.getRemark());
        queryWrapper.orderByDesc("outputtime");

        this.outportService.page(page,queryWrapper);
        List<Outport> records = page.getRecords();
        for (Outport outport : records){
            Provider provider = this.providerService.getById(outport.getProviderid());
            if(null != provider){
                outport.setProvidername(provider.getProvidername());
            }

            Goods goods = this.goodsService.getById(outport.getGoodsid());
            if(null != goods){
                outport.setGoodsname(goods.getGoodsname());
                outport.setSize(goods.getSize());
            }
        }
        return new DataGridView(page.getTotal(),records);
    }
}
